import java.sql.*;

/**
 * @description:
 * @author: fengfazheng
 * @create: 2021-12-16 02:37
 **/
public class QueryHiveUtils {
    // set the impalad host
    private static final String IMPALAD_HOST = "192.168.73.129";

    // port 21050 is the default impalad JDBC port
    private static final String IMPALAD_JDBC_PORT = "10000";

    private static final String CONNECTION_URL = "jdbc:hive2://" + IMPALAD_HOST + ':' + IMPALAD_JDBC_PORT + "/test";

    private static final String JDBC_DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";

    public static void test() {
        System.out.println("\n=============================================");
        System.out.println("Cloudera Impala JDBC Example");
        System.out.println("Using Connection URL: " + CONNECTION_URL);

        Connection con = null;

        try {

            Class.forName(JDBC_DRIVER_NAME);

            con = DriverManager.getConnection(CONNECTION_URL,"root","root");
            PreparedStatement  stmt = con.prepareStatement("SELECT * FROM test");
            ResultSet rs = stmt.executeQuery();
            System.out.println("\n== Begin Query Results ======================");
            // print the results to the console
            while (rs.next()) {
                // the example query returns one String column
                System.out.print(rs.getString("id") + "\t");
                System.out.print(rs.getString("name") + "\t");
            }

            System.out.println("== End Query Results =======================\n\n");

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
                // swallow
            }
        }
    }

//    public static void insert(Connection con) {
//        String sql = " insert into student2 select id,name,address from student";
//        try {
//            PreparedStatement  stmt = con.prepareStatement(sql);
//            stmt.execute();
//        } catch (SQLException e) {
//// TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) {
        test();
    }
}
